//
//  KWTPolicyListCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/15.
//

#import "KWTPolicyListCell.h"
#import "KWTVppSetRecordModel.h"
#import "RHTools.h"

@interface KWTPolicyListCell()
@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,strong) QMUILabel *stateLab;

@property (nonatomic ,strong) QMUILabel *groupNameLab;

@property (nonatomic ,strong) QMUILabel *modeLab;

@property (nonatomic ,strong) QMUILabel *typeLab;

@property (nonatomic ,strong) QMUILabel *timeLab;

@property (nonatomic ,strong) QMUILabel *superLab;

@property (nonatomic ,strong) QMUILabel *superRoleLab;

@property (nonatomic ,strong) MASConstraint *bot_constraint;

@property (nonatomic ,strong) UIView *bgView;

@end
@implementation KWTPolicyListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setRecordModel:(KWTVppSetRecordModel *)recordModel
{
    _recordModel = recordModel;
    self.modeLab.text = @"";
    self.groupNameLab.text = ValidNullDataToString(recordModel.groupName, @"--");
    
    self.timeLab.text = [NSDate dateStringWithTimeInterval:recordModel.createdTime/1000 formatter:@"yyyy-MM-dd HH:mm:ss"];
    self.typeLab.text = [NSString stringWithFormat:@"%@/%@",recordModel.productTypeCode,recordModel.deviceTypeCode];
    switch (recordModel.status) {
        case 0:
            {
                self.stateLab.text = @"下发中".rh_localized;
                self.stateLab.textColor = kCOLOR_HEX(@"#405DBE");
            }
            break;
        case 1:
            {
                self.stateLab.text = @"成功".rh_localized;
                self.stateLab.textColor = kCOLOR_HEX(@"#68D58F");
            }
            break;
        case 2:
            {
                self.stateLab.text = @"部分成功".rh_localized;
                self.stateLab.textColor = kCOLOR_HEX(@"#EFC633");
            }
            break;
        case 3:
            {
                self.stateLab.text = @"失败".rh_localized;
                self.stateLab.textColor = kCOLOR_HEX(@"#FB6868");
            }
            break;
        default:
            break;
    }
    
  
    [recordModel.paraArr enumerateObjectsUsingBlock:^(ParaModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.paramName isEqualToString:@"Inv3_AdvaMode"]) {
            switch ([model.value integerValue]) {
                case 0:
                    self.modeLab.text = @"禁用".rh_localized;
                    break;
                case 1:
                    self.modeLab.text = @"卖电优先".rh_localized;
                    break;
                case 2:
                    self.modeLab.text = @"自消耗卖电".rh_localized;
                    break;
                case 3:
                    self.modeLab.text = @"自消耗防逆流".rh_localized;
                    break;
                
                default:
                    break;
            }
        }
        
        if ([model.paramName isEqualToString:@"EMU_CutPeaks_Fillvalleys"]) {
            // 233的
            // 模式 0：无能量调度策略;1：自发自用 SGSC;2：削峰填谷 PeakShaving;3：光伏优先 SolarPriority;4：市电优先 GridPriority;5：备电模式 BkpPwrMode;6：电池模式（充放电调度） RmtDisPatchMode
            switch ([model.value integerValue]) {
                case 0:
                    self.modeLab.text = @"削峰填谷停止".rh_localized;
                    break;
                case 1:
                    self.modeLab.text = @"削峰填谷启动".rh_localized;
                    break;
              
                default:
                    break;
            }
        }
        
        if ([model.paramName isEqualToString:@"Inv5_Mode"]) {
            // 储充一体
            //  //工作模式 0:自发自用 1:备电模式 2:PV全额上网模式 3:PV+Bat全额上网模式 4:VPP模式 5:TOU模式
            switch ([model.value integerValue]) {
                case 0:
                    self.modeLab.text = @"自发自用".rh_localized;
                    break;
                case 1:
                    self.modeLab.text = @"备电模式".rh_localized;
                    break;
                case 2:
                    self.modeLab.text = @"PV全额上网模式".rh_localized;
                    break;
                case 3:
                    self.modeLab.text = @"PV+Bat全额上网模式".rh_localized;
                    break;
                case 4:
                    self.modeLab.text = @"VPP模式".rh_localized;
                    break;
                case 5:
                    self.modeLab.text = @"TOU模式".rh_localized;
                    break;
                default:
                    break;
            }
        }
        

    }];
    kWeakSelf;
    //别人对自己设置的
    if (self.recordModel.type == 2) {
        
        self.superLab.text = self.recordModel.superiorUser;
        //待完成
//        self.superRoleLab.text = [RHTools getRoleContentWithType:self.recordModel.superHierarchyCode ];
        self.superRoleLab.text = @"--";
        [self.bot_constraint uninstall];
        UIView *supierView = [self.bgView viewWithTag:303];
        UIView *supierRoleView = [self.bgView viewWithTag:304];
        supierView.hidden = NO;
        supierRoleView.hidden = NO;
        [supierRoleView mas_updateConstraints:^(MASConstraintMaker *make) {
          weakSelf.bot_constraint = make.bottom.mas_offset(-10);
        }];
    }else{//自己设置的
        [self.bot_constraint uninstall];
        UIView *timeView = [self.bgView viewWithTag:302];
        UIView *supierView = [self.bgView viewWithTag:303];
        UIView *supierRoleView = [self.bgView viewWithTag:304];
        supierView.hidden = YES;
        supierRoleView.hidden = YES;
        [timeView mas_updateConstraints:^(MASConstraintMaker *make) {
            weakSelf.bot_constraint = make.bottom.mas_offset(-10);
        }];
    }
    if ([recordModel.deviceTypeCode is61DeviceType]) {
        self.modeLab.text = @"TOU模式下发策略".rh_localized;
    }
}

-(void)hideArrow
{
    self.arrowImg.hidden = YES;
    [self.arrowImg mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(0);
    }];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView.layer setCornerRadius:8];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(7, 15, 7, 15));
    }];

    _groupNameLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#171A1D") textAligment:NSTextAlignmentLeft];
    _groupNameLab.numberOfLines = 2;
    [bgView addSubview:_groupNameLab];
    [_groupNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    _arrowImg = [[UIImageView alloc]initWithImage:kImage(@"arrow_2")];
    [bgView addSubview:_arrowImg];
    [_arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.width.height.offset(18);
        make.centerY.equalTo(weakSelf.groupNameLab);
    }];
    
    _stateLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#171A1D") textAligment:NSTextAlignmentRight];
    _stateLab.numberOfLines = 2;
    [bgView addSubview:_stateLab];
    [_stateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.arrowImg);
        make.right.equalTo(weakSelf.arrowImg.mas_left).offset(-3);
        make.width.lessThanOrEqualTo(@(100));
        make.left.equalTo(weakSelf.groupNameLab.mas_right).offset(5);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(1);
        make.top.equalTo(weakSelf.groupNameLab.mas_bottom).offset(15);
    }];
    
    __block UIView *lastView;
    NSArray *titles = @[@"模式",@"类型",@"时间",@"设置账户",@"设置角色"];
    
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [self itemViewWithTitle:title index:idx];
        [bgView addSubview:itemView];
        if (lastView) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(lastView);
                make.top.equalTo(lastView.mas_bottom);
            }];
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_offset(0);
                make.top.equalTo(line.mas_bottom).offset(10);
            }];
        }
        lastView = itemView;
    }];
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        weakSelf.bot_constraint = make.bottom.mas_offset(-10);
    }];
    self.modeLab = [bgView viewWithTag:200];
    self.typeLab = [bgView viewWithTag:201];
    self.timeLab = [bgView viewWithTag:202];
    self.superLab = [bgView viewWithTag:203];
    self.superRoleLab = [bgView viewWithTag:204];
}

-(UIView*)itemViewWithTitle:(NSString*)title index:(NSInteger)index
{
    UIView *bgView = kView;
    bgView.tag = 300+index;
    bgView.backgroundColor = UIColor.clearColor;
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D",0.6)];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.text = [NSString stringWithFormat:@"%@:",title.rh_localized];
    titleLab.tag = 100+index;
    titleLab.numberOfLines = 2;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(15);
        make.width.lessThanOrEqualTo(@(100));
        make.bottom.mas_offset(-10);
    }];
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"171A1D") textAligment:NSTextAlignmentRight];
    contentLab.tag = 200+index;
    [bgView addSubview:contentLab];
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_right).offset(5);
        make.right.mas_offset(-15);
        make.centerY.equalTo(titleLab);
    }];
    
    return bgView;
}

@end
